package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.one.bo.MajorBo;
import com.one.bo.RoleBo;
import com.one.dto.*;
import com.one.entity.*;
import com.one.enums.IsDeletedEnum;
import com.one.enums.SexEnum;
import com.one.enums.StatusEnum;
import com.one.mapper.*;
import com.one.service.SysMajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.service.SysUserService;
import com.one.utils.DataCheckUtils;
import com.one.utils.DateUtils;
import com.one.utils.PaginationUtils;
import com.one.utils.TreeUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fr
 * @since 2021-10-12
 */
@Service
public class SysMajorServiceImpl extends ServiceImpl<SysMajorMapper, SysMajorEntity> implements SysMajorService {

    private final SysMajorMapper sysMajorMapper;
    private final SysCollegeMapper sysCollegeMapper;
    private final SysRoleMapper sysRoleMapper;
    private final UserMenuMapper userMenuMapper;
    private final SysMenuMapper sysMenuMapper;
    private final RoleMenuMapper roleMenuMapper;
    private final SysUserMapper sysUserMapper;
    private final UserRoleMapper userRoleMapper;
    public SysMajorServiceImpl(SysCollegeMapper sysCollegeMapper,SysMajorMapper sysMajorMapper,SysRoleMapper sysRoleMapper,SysMenuMapper sysMenuMapper,SysUserMapper sysUserMapper,RoleMenuMapper roleMenuMapper,UserRoleMapper userRoleMapper,UserMenuMapper userMenuMapper) {
        this.sysMajorMapper = sysMajorMapper;
        this.sysRoleMapper = sysRoleMapper;
        this.sysMenuMapper = sysMenuMapper;
        this.sysUserMapper = sysUserMapper;
        this.roleMenuMapper = roleMenuMapper;
        this.userRoleMapper = userRoleMapper;
        this.userMenuMapper = userMenuMapper;
        this.sysCollegeMapper = sysCollegeMapper;
    }


    /**
     * 查询系部
     * @param majorBo
     * @return
     */
    @Override
    public Result selectMajor(MajorBo majorBo) {
        //数据校验
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(majorBo.getNumber(), majorBo.getPage()), "分页参数错误!");
        int page = majorBo.getPage();
        majorBo.setPage(PaginationUtils.getPageIndex(majorBo.getPage(), majorBo.getNumber()));
        //查询项目组评分 mybatis
        List<Object> result = sysMajorMapper.selectMajor(majorBo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(majorBo.getNumber(), page, sum);
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        List<SysMajorEntity> sysUserEntities =  (List<SysMajorEntity>) result.get(0);
        return Result.ok().data(new ReturnDate(paginationDate,sysUserEntities ));
    }


    /**
     * 删除系部
     * @param majorBo
     * @return
     */

    @Override
    public Result deleteMajor(MajorBo majorBo) {

        SysMajorEntity sysMajorEntity = new SysMajorEntity();
        sysMajorEntity.setIsDeleted(IsDeletedEnum.YES.getKey());
        UpdateWrapper<SysMajorEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysMajorEntity::getId, majorBo.getId()).eq(SysMajorEntity::getIsDeleted,IsDeletedEnum.NO.getKey());
        int row = sysMajorMapper.update(sysMajorEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除用户失败");
        }
        return Result.ok();
    }

    /**
     * 添加系部
     * @param sysMajorEntity
     * @return
     */
    @Override
    public Result addMajor(SysMajorEntity sysMajorEntity) {
        //数据校验
        //查询指定工号、身份证号、手机号是否重复
        //插入数据
        sysMajorEntity.setIsDeleted("0");
//        sysMajorEntity.setCode("1");
        sysMajorEntity.setCreateBy("管理员");
        sysMajorEntity.setCreateTime(DateUtils.getCurrentTime());
        int row = sysMajorMapper.insert(sysMajorEntity);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("添加系部失败");
        }
        return Result.ok();
    }

    /**
     * 编辑修改系部
     * @param sysMajorEntity
     * @return
     */
    @Override
    public Result updateMajor(SysMajorEntity sysMajorEntity) {
        sysMajorEntity.setUpdateBy("管理员");
        sysMajorEntity.setUpdateTime(DateUtils.getCurrentTime());
        UpdateWrapper<SysMajorEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysMajorEntity::getId, sysMajorEntity.getId());
        int row = sysMajorMapper.update(sysMajorEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑角色信息失败");
        }
        return Result.ok();
    }


    /**
     * 新增所属学院
     * @return
     */
    @Override
    public Result getcollegeCode() {
        QueryWrapper<SysCollegeEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(SysCollegeEntity::getIsDeleted, IsDeletedEnum.NO).select(SysCollegeEntity::getId,SysCollegeEntity::getName);
        List<SysCollegeEntity> semesterInfoEntities = sysCollegeMapper.selectList(sectionQueryWrapper);
        if (semesterInfoEntities.size() == 0) {
            return Result.error().message("没有学院");
        }
        List<EnumResult> result = semesterInfoEntities.stream().map(
                entry -> new EnumResult(entry.getId().toString(), entry.getName())).collect(Collectors.toList());
        return Result.ok().data(result);
    }

    /**
     * 新增教师
     * @return
     */
    @Override
    public Result getceachCode() {
        QueryWrapper<SysUserEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(SysUserEntity::getIsDeleted, IsDeletedEnum.NO).select(SysUserEntity::getId,SysUserEntity::getName);
        List<SysUserEntity> semesterInfoEntities = sysUserMapper.selectList(sectionQueryWrapper);
        if (semesterInfoEntities.size() == 0) {
            return Result.error().message("没有学院");
        }
        List<EnumResult> result = semesterInfoEntities.stream().map(
                entry -> new EnumResult(entry.getId().toString(), entry.getName())).collect(Collectors.toList());
        return Result.ok().data(result);
    }



//    @Override
//    public Result updateMajorMenu(MajorBo majorBo) {
//        QueryWrapper<MajorMenuEntity> wrapper = new QueryWrapper<>();
//        wrapper.lambda().eq(MajorMenuEntity::getMajorId,majorBo.getId());
//        majorMenuMapper.delete(wrapper);
//        if(majorBo.getMenuIds()==null || majorBo.getMenuIds().equals("")){
//            return Result.ok();
//        }
//        String[] menuIds = majorBo.getMenuIds().split(",");
//        MajorMenuEntity majorMenuEntity = new MajorMenuEntity();
//        majorMenuEntity.setMajorId(majorBo.getId());
//        for(String menu:menuIds){
//            majorMenuEntity.setMenuId(menu);
//            majorMenuMapper.insert(majorMenuEntity);
//        }
//        return Result.ok();
//    }


    /**
     * 按创建者查询系部
     * @param
     * @return
     */
//    @Override
//    public Result selectUserMajor(MajorBo majorBo) {
//        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(majorBo.getNumber(), majorBo.getPage()), "分页参数错误!");
//        int page = majorBo.getPage();
//        majorBo.setPage(PaginationUtils.getPageIndex(majorBo.getPage(), majorBo.getNumber()));
//        List<Object> result = null;
//        if("0".equals(majorBo.getUserType())){
//            result =  sysUserMapper.selectUserMajor(majorBo);
//        }else {
//            String userIds =  userMajorMapper.selectUserMajor(majorBo.getId());
//            majorBo.setUserIds(userIds);
//            result = sysUserMapper.selectUserMajor1(majorBo);
//        }
//        int sum = ((List<Integer>) result.get(1)).get(0);
//        //记录总数为0
//        if (sum == 0) return Result.error().message("查询结果为空");
//        PaginationDate paginationDate = PaginationUtils.getPaginationDate(majorBo.getNumber(), page, sum);
//        //当前页大于总页数
//        if (PaginationUtils.isValid(paginationDate))
//            return Result.error().message("分页参数错误!");
//        List<SysUserEntity> sysUserEntities =  (List<SysUserEntity>) result.get(0);
//        for (SysUserEntity sysUserEntity:sysUserEntities){
//            sysUserEntity.setSex(SexEnum.getEnumValue(sysUserEntity.getSex()));
//            sysUserEntity.setStatus(StatusEnum.getEnumValue(sysUserEntity.getStatus()));
//        }
//        return Result.ok().data(new ReturnDate(paginationDate,sysUserEntities ));
//    }

//    @Override
//    public Result cancelMajorUser(MajorBo majorBo) {
//        QueryWrapper<UserMajorEntity> wrapper = new QueryWrapper<>();
//        int result = 0;
//        if("0".equals(majorBo.getUserType())){
//            wrapper.lambda().eq(UserMajorEntity::getRoleId,majorBo.getId()).eq(UserMajorEntity::getUserId,majorBo.getUserId());
//            result = userMajorMapper.delete(wrapper);
//        }else {
//            UserMajorEntity userMajorEntity = new UserMajorEntity();
//            userMajorEntity.setMajorId(majorBo.getId());
//            userMajorEntity.setUserId(majorBo.getUserId());
//            result = userMajorMapper.insert(userMajorEntity);
//        }
//        //记录总数为0
//        if (result == 0) return Result.error().message("操作失败");
//        return Result.ok();
//    }

    @Override
    public Result getMenu(String userID) {
        QueryWrapper<UserMenuEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(UserMenuEntity::getUserId, userID);
        List<UserMenuEntity> result = userMenuMapper.selectList(sectionQueryWrapper);
        if (result.size() == 0) {
            return Result.error().message("没有拥有权限的菜单！");
        }
        List<UserMenu> userMenus =  TreeUtils.getUserMenuTree(result,"0");
        return Result.ok().data(userMenus);
    }



}
